package com.comdo.newup_loan.module.repayment_action;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.model.ToRepayBean;
import com.comdo.newup_loan.module.recharge.MineTopUpActivity;
import com.comdo.newup_loan.util.StringUtils;
import com.comdo.newup_loan.util.TitleMenuUtil;

import butterknife.BindView;

/**
 * Created by liyuan on 2017/11/6.
 */

public class RepayActivity extends BaseActivity<RepayPersenter> implements RepayContact.View{
    @BindView(R.id.tv_left_amount)
    TextView mLeftAmountText;
    @BindView(R.id.tv_should_pay)
    TextView mShouldPayText;
    @BindView(R.id.btn_sumbit)
    Button mSubmitBtn;
    @BindView(R.id.ll_recharge)
    LinearLayout mRechargeLin;
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_repayment_repay;
    }

    @Override
    protected void init() {
        super.init();

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        new TitleMenuUtil(RepayActivity.this,"还款",true);
        mSubmitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.rePay(getIntent().getIntExtra("repayPlanId",0), Config.loginInfo.getUserId());
            }
        });
        mRechargeLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(RepayActivity.this, MineTopUpActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public RepayPersenter getmPresenter() {
        return new RepayPersenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void getFail(String failMessage) {
        showToast(failMessage);
    }

    @Override
    public void getData(ToRepayBean datas) {
        mLeftAmountText.setText(StringUtils.getMoneyString(datas.getAvailable()));
        mShouldPayText.setText(StringUtils.getMoneyString(datas.getSupportBackAmount()));
        if(datas.getSupportBackAmount()>datas.getAvailable()){
            mSubmitBtn.setText("余额不足");
            mSubmitBtn.setEnabled(false);
        }else {
            mSubmitBtn.setText("还款");
            mSubmitBtn.setEnabled(true);
        }
    }

    @Override
    public void rePay(String result) {
        showToast(result);
        this.finish();
    }
    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getData(getIntent().getIntExtra("repayPlanId",0), Config.loginInfo.getUserId());
    }
}
